const { wxml, style } = require('./index.js')
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    src: '',
    width:'',
    height:'',
    showCanvas:false,
    showPlii:false,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    ProductShare(e) {
      let that = this;
      if(that.data.src.length>0){
        this.setData({
          showPlii: true
        })
      }else{
          that.widget = that.selectComponent('.widget');
          let shareid = app.functions.getLocalStorage("shareUserId");
          let shop = app.functions.getLocalStorage("shop");
          // let params = {
          //   centerId: shop.centreId,
          //   teamId: shop.id,
          //   productId: e.id,
          //   shareId: shareid?shareid:'',
          //   page: "pages_subpack/goods_detail/goods_detail"
          // };
          // app.functions.loadingToast("生成中...");
          // app.api.shareMatter(params, (response) => {
          //   console.info("接口返回",response);
          //   let goods = response.data;
          //   that.render(goods);
          // }, (error) => {

          // })
          setTimeout(()=>{
            that.render(app.data.productDetail);
          },2000)
      }
    },
    render(goods) {
      let that = this;
      let img=goods.thumbSquare ? goods.thumbSquare : goods.thumb;
      let _wxml = wxml(app.company,img.replace('http://', 'https://') , goods.title, goods.marketprice.toFixed(2), goods.productprice.toFixed(2), goods.quickMarkUrl.replace('http://', 'https://'));
      const p1 = that.widget.renderToCanvas({ wxml: _wxml, style })
      p1.then((res) => {
        that.container = res;
        wx.nextTick(() => {
          that.extraImage();
        })
      })
    },
    extraImage() {
      const p2 = this.widget.canvasToTempFilePath()
      p2.then(res => {
        console.info("生成完成",res);
        this.setData({
          showPlii: true,
          src: res.tempFilePath,
          width: this.container.layoutBox.width,
          height: this.container.layoutBox.height
        })
      })
    },
    //保存
    save() {
      let that=this;
      wx.saveImageToPhotosAlbum({
        filePath: this.data.src,
        success: function () {
          wx.showToast({
            title: "保存成功",
            icon: "success",
          });
          that.setData({
            showPlii: false
          })
        }
      })
    },
    onClosePill() {
      this.setData({
        showPlii: false
      })
    }
  }
})
